package com.sss.springcloud.service.impl;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sss.springcloud.common.Result;
import com.sss.springcloud.entities.Orders;
import com.sss.springcloud.dao.IOrdersDao;
import com.sss.springcloud.service.AccountService;
import com.sss.springcloud.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sss.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sss
 * @since 2020-03-13
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<IOrdersDao, Orders> implements IOrdersService {
    private static final Logger log = LoggerFactory
            .getLogger(OrdersServiceImpl.class);

    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @GlobalTransactional(name = "order-sss",rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean create(Orders order) {
        log.info("*****开始创建新订单");
        // 1新建订单
        order.setStatusd(0);
        save(order);

        log.info("------->订单微服务开始调用库存，作扣减");
        // 2 扣库存
        storageService.decrease(order.getProductId(),order.getCounts());
        log.info("------->订单微服务开始调用库存，作扣减END");

        log.info("------->账户微服务开始调用库存，作扣减");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("------->账户微服务开始调用库存，作扣减END");

        //修改订单状态。从0到1, 1 代表已完成
        log.info("------->修改订单状态开始");
        order.setStatusd(1);
        updateById(order);
        log.info("------->修改订单状态结束");
        log.info("------->下订单结束了,O(∩_∩)O哈哈~");
        return true;
    }

}
